<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:43
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;
use think\Loader;

class Banner extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('banner')
                ->field('b.id, b.banner_category_id, b.title, b.thumb, b.url, b.update_time, b.status, b.sort, b.create_time, c.title as category_name')
                ->alias('b')
                ->join('__BANNER_CATEGORY__ c', 'b.banner_category_id = c.id')
                ->order('b.sort desc, b.create_time desc')
                ->select();
            return view('index', [
                'list'=>$list
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            $cate = Db::name('banner_category')
                ->field('id, title')
                ->select();
            return view('add', [
                'cate'=>$cate
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $data                       = [];
            $data['banner_category_id'] = intval(input('param.banner_category_id'));
            $data['title']              = input('param.title');
            $data['url']                = input('param.url');
            $data['sort']               = intval(input('param.sort'));
            $data['status']             = intval(input('param.status'));
            $data['create_time']        = time();
            $data['update_time']        = $data['create_time'];
            $info                       = img_upload_one('thumb');
            if ($info)
            {
                if ($info['status'])
                {
                    $data['thumb'] = $info['msg'];
                } else {
                    return ['status'=>false, 'msg'=>$info['msg']];
                }
            }
            $res = Db::name('banner')->insertGetId($data);
            if ($res)
            {
                return ['status'=>true, 'msg'=>'添加成功'];
            } else {
                return ['status'=>false, 'msg'=>'添加失败'];
            }
        }
    }

    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id   = intval(input('param.id'));
            $info = Db::name('banner')
                ->field('id, banner_category_id, title, thumb, url, sort, status')
                ->where('id', 'eq', $id)
                ->find();
            $cate = Db::name('banner_category')->field('id, title')->select();
            return view('edit', [
                'cate'=>$cate,
                'info'=>$info
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doEdit()
    {
        if (request()->isPost())
        {
            $id                         = intval(input('param.id'));
            $banner                     = Db::name('banner')->field('thumb')->where('id', 'eq', $id)->find();
            $data                       = [];
            $data['banner_category_id'] = intval(input('param.banner_category_id'));
            $data['title']              = input('param.title');
            $data['url']                = input('param.url');
            $data['sort']               = intval(input('param.sort'));
            $data['status']             = intval(input('param.status'));
            $data['update_time']        = time();
            $info                       = img_upload_one('thumb');
            if ($info)
            {
                if ($info['status'])
                {
                    $data['thumb'] = $info['msg'];
                    if (!empty($banner['thumb']))
                    {
                        $path = './public/'.$banner['thumb'];
                        if (file_exists($path))
                        {
                            @unlink($path);
                        }
                    }
                } else {
                    return ['status'=>false, 'msg'=>$info['msg']];
                }
            }
            $res = Db::name('banner')->where('id', 'eq', $id)->update($data);
            if ($res)
            {
                return true;
            } else {
                return false;
            }
        }
    }

    public function del()
    {
        if (request()->isPost())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id   = intval(input('param.id'));
                $info = Db::name('banner')
                    ->field('thumb')
                    ->where('id', 'eq', $id)
                    ->find();
                $res = Db::name('banner')->where('id', 'eq', $id)->delete();
                if ($res)
                {
                    if (!empty($info['thumb']))
                    {
                        $path = './public/'.$info['thumb'];
                        if (file_exists($path))
                        {
                            @unlink($path);
                        }
                    }
                    return ['status'=>true, 'msg'=>'修改成功'];
                } else {
                    return ['status'=>false, 'msg'=>'修改失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }
        }
    }

}